// 定制请求的实例
import axios from 'axios';
import { useUserInfoStore } from '@/stores/userInfo.js';
const userInfoStore = useUserInfoStore();
const baseURL = '/api';
const instance = axios.create({ baseURL });

// 添加响应拦截器
instance.interceptors.response.use(
  result => {
    return result.data;
  },
  error => {
    if(error.response.status === 401){
      // 拦截器这里视401为请求成功，具体处理由业务决定
      return error.response.data;
    }
    console.error('Request Error:', error);
    return Promise.reject(error);
  }
);


// 添加请求拦截器
instance.interceptors.request.use(
  config => {
    if(userInfoStore.userInfo.token!=null){
      config.headers.Authorization = `Bearer ${userInfoStore.userInfo.token}`;
    }
    return config;
  },
  error => {
    console.error('Request Error:', error);
    return Promise.reject(error);
  }
);

export default instance;